package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.entity.CxyCollection;
import com.cd.javacxy.common.model.entity.SessionUser;
import com.cd.javacxy.common.model.enums.ResponseCode;
import com.cd.javacxy.common.model.query.CollectionQuery;
import com.cd.javacxy.common.model.vo.AjaxResponse;
import com.cd.javacxy.common.model.vo.CxyUserVo;
import com.cd.javacxy.common.utils.Constants;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.web.service.CxyCollectionService;
import com.cd.javacxy.module.web.service.CxyUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * CxyCollection的路由接口服务
 *
 * @author yanghy
 */
@Slf4j
@Controller
@RequestMapping("/collection")
public class CxyCollectionController {

    /**
     * CxyCollectionService服务
     */
    private CxyCollectionService cxyCollectionService;

    private CxyUserService userService;

    @Autowired
    public void setCxyCollectionService(CxyCollectionService cxyCollectionService) {
        this.cxyCollectionService = cxyCollectionService;
    }

    @Autowired
    public void setUserService(CxyUserService userService) {
        this.userService = userService;
    }

    @GetMapping("/list")
    public ModelAndView collectionList(HttpSession session, CollectionQuery collectionQuery) {
        ModelAndView view = new ModelAndView("/page/admin/collectionList");
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            view.setViewName("redirect:" + Constants.LOGINABSOLUTEPATH);
            return view;
        }
        try {
            CxyUserVo user = this.userService.findUserInfo4UserHome(sessionUser.getUserId());
            view.addObject("user", user);
            view.addObject("articleType", collectionQuery.getArticleType().getType());
        } catch (Exception e) {
            log.error("获取收藏列表失败", e);
            view.setViewName("redirect:" + Constants.ERROR_404);
            return view;
        }
        return view;
    }

    @ResponseBody
    @GetMapping("/load")
    public AjaxResponse<Object> loadCollection(CollectionQuery collectionQuery) {
        PageResult<CxyCollection> result = this.cxyCollectionService.findCollectionByPage(collectionQuery);
        return AjaxResponse.builder().data(result).code(ResponseCode.SUCCESS.getCode()).build();

    }

    @ResponseBody
    @DeleteMapping("/del")
    public AjaxResponse<Object> delCollection(CxyCollection collection) {
        int r = this.cxyCollectionService.deleteCollection(collection);
        if (r > 0) {
            return AjaxResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        }
        return AjaxResponse.builder().code(ResponseCode.BUSSINESSERROR.getCode()).build();
    }


    @ResponseBody
    @RequestMapping("/pulish")
    public AjaxResponse<Object> doCollection(HttpSession session, CxyCollection collection){
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if(sessionUser==null){
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        try {
            this.cxyCollectionService.addCollection(collection);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (BussinessException e) {
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            log.error("{}收藏出错{}", sessionUser.getUserName(), e.getLocalizedMessage());
        }catch (Exception e) {
            log.error("{}收藏出错{}", sessionUser.getUserName(), e.getLocalizedMessage());
            ajaxResponse.setErrorMsg("服务器出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }
}
